﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace QuickWPFMonthCalendar
{
    /// <summary>
    /// Interaction logic for viewDiary.xaml
    /// </summary>
    public partial class viewDiary : Window
    {
        System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        private int viewImageId = 0;
        DB.Tables.Diary Itemdiary=new DB.Tables.Diary();
        List<DB.Tables.Images> ListImages= new List<DB.Tables.Images>();
        private DB.Tables.Settings Settings = new DB.Tables.Settings();
        public viewDiary(DB.Tables.Diary diary,List<DB.Tables.Images> listImages )
        {
            InitializeComponent();

            txtBody.Text = diary.Body;

            this.Title = diary.DateFormat.Substring(0,10);
            bltitle.Text = diary.DateFormat.Substring(0, 10);

            Itemdiary = diary;

            ListImages = listImages;

            if (ListImages.Count > 0)
            {
                viewImage(0);
                
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
                dispatcherTimer.Start();
            
            }

            GetSettings();
        }

        private void viewImage(int id)
        {

            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);


            BitmapImage bitImage = new BitmapImage();
            bitImage.BeginInit();
            Uri uri = new Uri(path + @"\"+ ListImages[id].Path, UriKind.RelativeOrAbsolute);
            bitImage.UriSource = uri;
            bitImage.EndInit();

            imgShow.Source = bitImage;
        }

        private void btnNex_Click(object sender, RoutedEventArgs e)
        {
            if ((viewImageId + 1)<ListImages.Count)
            {
                viewImageId++;
                viewImage(viewImageId);
            }

        }

        private void btnPre_Click(object sender, RoutedEventArgs e)
        {
            if ((viewImageId - 1) >= 0 && ListImages.Count>0)
            {
                viewImageId--;
                viewImage(viewImageId);
            }
        }


        //  System.Windows.Threading.DispatcherTimer.Tick handler
        //
        //  Updates the current seconds display and calls
        //  InvalidateRequerySuggested on the CommandManager to force 
        //  the Command to raise the CanExecuteChanged event.
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if ((viewImageId + 1) < ListImages.Count)
            {
                viewImageId++;
                viewImage(viewImageId);
            }
            else {
                viewImageId=0;
                viewImage(viewImageId);
            }
        }


        /// <summary>
        /// get settings
        /// </summary>
        private void GetSettings()
        {

            DB.Execute execute = new DB.Execute();
            DataSet ds = execute.Select_tblSettings();
            foreach (DataTable dt in ds.Tables)
                foreach (DataRow dr in dt.Rows)
                    if (dr["Id"].ToString().ToUpper().Equals("COLOR"))
                        Settings.color = dr["value"].ToString();

            this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Settings.color));
            gdContainer.Background = this.Background;
        }
    }
}
